package com.ymatou.iapi.sellerquery.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerquery.parameter.GetRefundBillBean;
import com.ymatou.iapi.sellerquery.parameter.resp.RefundBillProductResp;
import com.ymatou.iapi.sellerquery.parameter.resp.RefundBillResp;
import com.ymatou.iapi.sellerquery.service.GetRefundBillCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.YmtRefundBill;
import com.ymttest.database.model.YmtRefundProduct;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtRefundWapper;
import com.ymttest.utils.constant.ECode;
/**
*退款单号获取退款信息接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetRefundBill{
	private static GetRefundBillBean getrefundbillBean;
	private static GetRefundBillCall getrefundbillCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退款单号获取退款信息接口");
	}

	@Before
	public void caseUp() {
		getrefundbillBean=new GetRefundBillBean();
		getrefundbillCall=new GetRefundBillCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getrefundbillCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRefundBill_001() {
		Logger.start(true,"退款单号获取退款信息成功");
		try {
	        DefaultWapper defaultWapper = new DefaultWapper();
            YmtRefundWapper ymtRefund = new YmtRefundWapper();
            List<YmtRefundBill> ymtRefundBillList=ymtRefund.selectRefundBill(null);
            //从数据库里随机取一个退款单号
            YmtRefundBill randomYmtRefundBill=ymtRefundBillList.get(new java.util.Random().nextInt(9));
           String refundNo=randomYmtRefundBill.getRefundBillNo();
           //退款的商品信息
           List<YmtRefundProduct> refundProductList= ymtRefund.selectRefundProductByRefundBillNo(refundNo);
           
           String param="RefundBillNo="+refundNo;
           getrefundbillCall.setData(param);
           getrefundbillCall.callService();
                   
       		List<RefundBillProductResp> refundBillProductRespList=new ArrayList();    		      		
       		RefundBillResp refundBillResp=getrefundbillCall.getRefundBillRespList();
 
           refundBillProductRespList=refundBillResp.getRefundBillProductList();
           
			Logger.verifyEquals(ECode.SUCCESS, getrefundbillCall.getCode(), "验证code");
			Logger.verifyEquals(0, getrefundbillCall.getBCode(), "验证bcode");
			Logger.verifyEquals(randomYmtRefundBill.getOrderId(), refundBillResp.getOrderId(), "验证orderId");
			Logger.verifyEquals(randomYmtRefundBill.getRefundBillNo(), refundBillResp.getRefundBillNo(), "验证refundNo");
			Logger.verifyEquals(randomYmtRefundBill.getRefundAmount(), refundBillResp.getRefundAmount(), "验证refundAmount");
			Logger.verifyEquals(randomYmtRefundBill.getRefundedAmountOfCash(), refundBillResp.getRefundedAmountOfCash(), "验证refundedAmountOfCash");
			Logger.verifyEquals(randomYmtRefundBill.getRefundedAmountOfGift(), refundBillResp.getRefundedAmountOfGift(), "验证refundedAmountOfGift");        
			Logger.verifyEquals(refundProductList.size(), refundBillProductRespList.size(), "验证接口返回的退款商品信息数量");
			
			for(int i=0;i<refundBillProductRespList.size();i++){		
				
				String orderInfoId=refundBillProductRespList.get(i).getOrderInfoId();
				List<Map> ymtOrderInfoList = defaultWapper.selectOrderInfoByOrderInfoId(orderInfoId);

		        BigDecimal fProductPrice = (BigDecimal) ymtOrderInfoList.get(0).get("fProductPrice");
		        int iAmount = (Integer) ymtOrderInfoList.get(0).get("iAmount");
		        BigDecimal fDiscount = (BigDecimal) ymtOrderInfoList.get(0).get("fDiscount");
		        BigDecimal fFreight = (BigDecimal) ymtOrderInfoList.get(0).get("fFreight");
		        BigDecimal fSellerPromotionAmount = (BigDecimal) ymtOrderInfoList.get(0).get("fSellerPromotionAmount");
		        BigDecimal fSellerCouponAmount = (BigDecimal) ymtOrderInfoList.get(0).get("fSellerCouponAmount");
		        BigDecimal fYmtCouponAmount = (BigDecimal) ymtOrderInfoList.get(0).get("fYmtCouponAmount");
				
				Logger.verifyEquals(iAmount, refundBillProductRespList.get(i).getAmount(), "验证amount");
				Logger.verifyEquals(fDiscount, refundBillProductRespList.get(i).getDiscount(), "验证discount");
				Logger.verifyEquals(fFreight, refundBillProductRespList.get(i).getFreight(), "验证freight");
				Logger.verifyEquals(refundBillProductRespList.get(i).getOrderInfoId(), refundBillProductRespList.get(i).getOrderInfoId(), "验证orderInfoId");
				
				Logger.verifyEquals(fProductPrice, refundBillProductRespList.get(i).getProductPrice(), "验证productPrice");
				Logger.verifyEquals(fSellerCouponAmount, refundBillProductRespList.get(i).getSellerCouponAmount(), "验证sellerCouponAmount");
				Logger.verifyEquals(fYmtCouponAmount, refundBillProductRespList.get(i).getYmtCouponAmount(), "验证ymtCouponAmount");        
				Logger.verifyEquals(refundBillProductRespList.get(i).getRefundBillNo(), refundBillProductRespList.get(i).getRefundBillNo(), "验证refundBillNo");        
				Logger.verifyEquals(refundBillProductRespList.get(i).getOrderId(), refundBillProductRespList.get(i).getOrderId(), "验证orderId");        
				Logger.verifyEquals(refundBillProductRespList.get(i).getRefundAmount(), refundBillProductRespList.get(i).getRefundAmount(), "验证refundAmount");        
				Logger.verifyEquals(refundBillProductRespList.get(i).getRefundedAmountOfCash(), refundBillProductRespList.get(i).getRefundedAmountOfCash(), "验证refundedAmountOfCash");        
				Logger.verifyEquals(refundBillProductRespList.get(i).getRefundedAmountOfGift(), refundBillProductRespList.get(i).getRefundedAmountOfGift(), "验证refundedAmountOfGift");        
			}
			Logger.verifyEquals(true, getrefundbillCall.getData().getBoolean("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetRefundBill_002() {
		Logger.start(true,"退款单号不存在");
		try {

           String refundNo="221";
           String param="RefundBillNo="+refundNo;
           getrefundbillCall.setData(param);
           getrefundbillCall.callService();
                   
       		List<RefundBillProductResp> refundBillProductRespList=new ArrayList();    		      		
       		RefundBillResp refundBillResp=getrefundbillCall.getRefundBillRespList();
 
           refundBillProductRespList=refundBillResp.getRefundBillProductList();
           
			Logger.verifyEquals(ECode.SUCCESS, getrefundbillCall.getCode(), "验证code");
			Logger.verifyEquals(0, getrefundbillCall.getBCode(), "验证bcode");
			Logger.verifyEquals(0, refundBillResp.getOrderId(), "验证orderId");
			Logger.verifyEquals(null, refundBillResp.getRefundBillNo(), "验证refundNo");
			Logger.verifyEquals(null, refundBillResp.getRefundAmount(), "验证refundAmount");
			Logger.verifyEquals(null, refundBillResp.getRefundedAmountOfCash(), "验证refundedAmountOfCash");
			Logger.verifyEquals(null, refundBillResp.getRefundedAmountOfGift(), "验证refundedAmountOfGift");        
			Logger.verifyEquals(0, refundBillProductRespList.size(), "验证接口返回的退款商品信息数量");			
			Logger.verifyEquals(true, getrefundbillCall.getData().getBoolean("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRefundBill_003() {
		Logger.start(true,"退款单号为null");
		try {
	        
			  String refundNo=null;
	           String param="RefundBillNo="+refundNo;
	           getrefundbillCall.setData(param);
	           getrefundbillCall.callService();
	                   
	       		List<RefundBillProductResp> refundBillProductRespList=new ArrayList();    		      		
	       		RefundBillResp refundBillResp=getrefundbillCall.getRefundBillRespList();
	 
	           refundBillProductRespList=refundBillResp.getRefundBillProductList();
	           
				Logger.verifyEquals(ECode.SUCCESS, getrefundbillCall.getCode(), "验证code");
				Logger.verifyEquals(0, getrefundbillCall.getBCode(), "验证bcode");
				Logger.verifyEquals(0, refundBillResp.getOrderId(), "验证orderId");
				Logger.verifyEquals(null, refundBillResp.getRefundBillNo(), "验证refundNo");
				Logger.verifyEquals(null, refundBillResp.getRefundAmount(), "验证refundAmount");
				Logger.verifyEquals(null, refundBillResp.getRefundedAmountOfCash(), "验证refundedAmountOfCash");
				Logger.verifyEquals(null, refundBillResp.getRefundedAmountOfGift(), "验证refundedAmountOfGift");        
				Logger.verifyEquals(0, refundBillProductRespList.size(), "验证接口返回的退款商品信息数量");			
				Logger.verifyEquals(true, getrefundbillCall.getData().getBoolean("success"), "验证success");			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}